﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class editProduct : System.Web.UI.Page
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Edit product";
            if (!IsPostBack)
            {
                int productID = Convert.ToInt32(Request.QueryString["id"]);
                loadDataCBLanguage();
                int laguageID=Convert.ToInt32(DropDownList2.SelectedValue);
                var productLanguage = dc.ProductsLanguages.Where(z => z.productsId == productID).ToList();
                var pro = dc.Products.SingleOrDefault(z => z.id == productID);
                
                string  s = pro.categoriesId.ToString();
                loadDatFOrTabpane(productLanguage);
                textboxPrice.Value = pro.price.ToString();
                textboxQuantity.Value = pro.quantity.ToString();
                textboxDiscount.Value = pro.discount.ToString();

                bool f = Convert.ToBoolean(pro.status);
                if (f)
                {
                    RadioButtonList1.Items[0].Selected = true;
                }
                else {
                    RadioButtonList1.Items[1].Selected = true;
                }

                loadCategory(laguageID,s);
                
                loadPartner(pro.PartnersId.Value);

                List<ProductsPhoto> prophoto = dc.ProductsPhotos.Where(z => z.productsId == productID).ToList();
                Repeater1.DataSource = prophoto;
                Repeater1.DataBind();

                loadDataforProductRelative(laguageID);
            }
           

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int productID = Convert.ToInt32(Request.QueryString["id"]);
            var pro = dc.Products.SingleOrDefault(z => z.id == productID);
            
            if (string.IsNullOrEmpty(TextboxName_en.Value) && string.IsNullOrEmpty(TextboxName_fr.Value) && string.IsNullOrEmpty(TextboxName_gr.Value))
            {
                Label_proname.Text = "Enter product name please, at least one language";
            }

            else {

                delete_prolanguage(pro.ProductsLanguages.ToList());
                //delete_Photo(pro.ProductsPhotos.ToList());
                //delete_proRelavtie(pro.ProductsRelatives.ToList());

                List<ProductsLanguage> listProductLanguage = new List<ProductsLanguage>();
                if (!string.IsNullOrEmpty(TextboxName_en.Value.Trim()))
                {
                    insertProLanguage(pro, TextboxName_en.Value, TextboxDescription_en.Value, 1);
                }
                if (!string.IsNullOrEmpty(TextboxName_fr.Value.Trim()))
                {
                    insertProLanguage(pro, TextboxName_fr.Value, TextboxDescriptionFr.Value, 2);
                }
                if (!string.IsNullOrEmpty(TextboxName_en.Value.Trim()))
                {
                    insertProLanguage(pro, TextboxName_gr.Value, TextboxDescriptionGr.Value, 3);
                }
                string photo1 = textboxURL1.Value.Trim();
                if (!string.IsNullOrEmpty(photo1))
                {
                    var productPhot = new ProductsPhoto
                    {
                        name=photo1,
                        productsId=productID,
                    };
                    dc.ProductsPhotos.InsertOnSubmit(productPhot);
                    dc.SubmitChanges();
                }


                //insert into product relative
                var prorelative = dc.ProductsRelatives.Where(z => z.productsId_1 == productID);
                if (prorelative!=null)
                {
                    dc.ProductsRelatives.DeleteAllOnSubmit(prorelative);
                    dc.SubmitChanges();
                }

                CheckBox tb = null;
                ListViewItem Item = null;
                List<int> listHiddenValue = new List<int>();
                foreach (ListViewDataItem item in ListView1.Items)
                {

                    Item = item;
                    tb = ((CheckBox)(Item.FindControl("CheckBox1")));
                    if (tb.Checked)
                    {
                        HiddenField hd = tb.FindControl("HiddenField1") as HiddenField;
                        listHiddenValue.Add(Convert.ToInt32(hd.Value));
                    }
                }

                foreach (int item in listHiddenValue)
                {
                    ProductsRelative pr = new ProductsRelative
                    {

                        productsId_1 = pro.id,
                        productsId_2 = item,
                    };

                    dc.ProductsRelatives.InsertOnSubmit(pr);
                    dc.SubmitChanges();
                }
                    pro.price =Convert.ToDecimal(textboxPrice.Value.Trim());
                    pro.quantity =Convert.ToInt32(textboxQuantity.Value.Trim());
                    pro.features = Convert.ToBoolean(Convert.ToInt32(RadioButtonList1.SelectedValue));
                    pro.categoriesId = Convert.ToInt32(DropDownList1.SelectedValue);
                    pro.discount = Convert.ToInt32(textboxDiscount.Value.Trim());
                    pro.PartnersId = Convert.ToInt32(DropDownList3.SelectedValue);
                    dc.SubmitChanges();
                    Response.Redirect("productList.aspx");

                }

            }

        

        private void insertProLanguage(Product cat, string name, string description, int languageId)
        {

            ProductsLanguage pl = new ProductsLanguage
            {

                details = description,
                name = name,
                languageId = languageId,
                productsId = cat.id
            };

            dc.ProductsLanguages.InsertOnSubmit(pl);
            dc.SubmitChanges();


        }
        
        private void delete_prolanguage(List<ProductsLanguage> list){

            foreach (var item in list)
            {
                dc.ProductsLanguages.DeleteOnSubmit(item);
                dc.SubmitChanges();
            }
        }

        
        private void loadDataforProductRelative(int idlanguage)
        {

            ListView1.DataSource = dc.ProductsLanguages.Where(z => z.languageId == idlanguage);
            ListView1.DataBind();
        }

        protected void DropDownList1_DataBound(object sender, EventArgs e)
        {
           
        }


        private void loadPartner(int selected)
        {
            DropDownList3.DataSource = dc.Partners.Where(z=>z.status==true);
            DropDownList3.DataTextField = "name";
            DropDownList3.DataValueField = "id";
            DropDownList3.DataBind();
            if (DropDownList3.Items.FindByValue(selected.ToString()) != null)
            {
                DropDownList3.Items.FindByValue(selected.ToString()).Selected = true;
            }
           
        }
        private void loadCategory(int language,string s) {
            DropDownList1.DataSource = dc.CategoriesLanguages.Where(z=>z.languageId==language);
           
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "categoriesId";
            DropDownList1.DataBind();

            DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(s));
           
           
           
        }

        private void loadDatFOrTabpane(List<ProductsLanguage> proList)
        {
            foreach (var item in proList)
            {
                if (item.languageId == 1)
                {
                    TextboxName_en.Value = item.name;
                    TextboxDescription_en.Value = item.details;
                }
                if (item.languageId == 2)
                {
                    TextboxName_fr.Value = item.name;
                    TextboxDescriptionFr.Value = item.details;

                }
                if (item.languageId == 3)
                {
                    TextboxName_gr.Value = item.name;
                    TextboxDescriptionGr.Value = item.details;
                }
            }
        
        }
        private void loadDataCBLanguage(){

            DropDownList2.DataSource = dc.Languages;
            DropDownList2.DataTextField = "name";
            DropDownList2.DataValueField = "id";
            DropDownList2.DataBind();

        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
             int productID = Convert.ToInt32(Request.QueryString["id"]);
            int idLanguage = Convert.ToInt32(DropDownList2.SelectedValue);
             var pro = dc.Products.SingleOrDefault(z => z.id == productID);
                
                string  s = pro.categoriesId.ToString();
            loadCategory(idLanguage,s);
            loadDataforProductRelative(idLanguage);
       
        }

    

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        { int productID = Convert.ToInt32(Request.QueryString["id"]);
  
            var list = dc.ProductsRelatives.Where(p => p.productsId_1 == productID);
            if (e.Item.ItemType==ListViewItemType.DataItem)
            {
                foreach (var item in list)
                {
                    CheckBox cb = e.Item.FindControl("CheckBox1") as CheckBox;
                    HiddenField hd = cb.FindControl("HiddenField1") as HiddenField;
                    if (item.productsId_2.Value==Convert.ToInt32(hd.Value))
                    {
                        cb.Checked = true;
                    }
                }
            }
        }

       
    }
}